package com.Gameplay.character.Action;

import com.Gameplay.character.Role.parentRole;
import com.Input.InputManager;
import com.core.Window.Main.Game;
import javafx.scene.input.KeyCode;

import static java.lang.Math.abs;

public class Moving1 {
    private double pushCurrentX;
    private final int ComputerHeight = Game.getInstance().getHeight();
    private final int ComputerWidth = Game.getInstance().getWidth();
    //角色移动速度
    private final double speedX = 10;
    private final double speedY = -2000;
    private double currentSpeedY = 0;
    //判断角色是否处于跳跃状态以及跳跃模式
    private boolean isjumping = false;
    private boolean isjumped = true;
    private int jumpMod = 0;

    private parentRole role;

    public Moving1(parentRole role) {
        this.role = role;
    }

    public void Moving(parentRole entity) {

        role.setisMoving(false);
        //判断角色面向
        role.facing(entity);
        //更新角色位置
        role.Moving1update();

        //水平移动
        if (!isjumping) {
            pushCurrentX = 0;
            switch (role.getFacing()) {
                case 0:
                    Facing0Move(entity);
                    break;
                case 1:
                    Facing1Move(entity);
                    break;
            }
        }

        //跳跃
        if (InputManager.getPressed().contains(KeyCode.W) && isjumped) {
            isjumped = false;
            jumpMod = 0;
            if (InputManager.getPressed().contains(KeyCode.A)) {
                jumpMod = 1;
            }
            if (InputManager.getPressed().contains(KeyCode.D)) {
                jumpMod = 2;
            }
            if (InputManager.getPressed().contains(KeyCode.A) && InputManager.getPressed().contains(KeyCode.D)) {
                jumpMod = 0;
            }
            if (role.getCurrentY() == (double) ComputerHeight / 2 + (double) ComputerHeight / 7) {
                isjumping = true;
                currentSpeedY = speedY;
            }
        }
        if (isjumping) {
            double time = (double) 1 / 20;
            double gravity = 4000;
            currentSpeedY += gravity * time;
            role.setCurrentY(role.getCurrentY() + currentSpeedY * time);
            switch (jumpMod) {
                case 0://垂直跳
                    VerticalJump(entity);
                    break;
                case 1://向左挑
                    LeftJump(entity);
                    break;
                case 2://向右跳
                    RightJump(entity);
                    break;
            }
        }

        role.getRoleBox().setBox(role.getCurrentX(), role.getCurrentY());

    }

    //facing为0时的移动方法(即朝向为左)
    public void Facing0Move(parentRole entity) {
        if (InputManager.getPressed().contains(KeyCode.A)
                && role.getRoleBox().getLwidth() >= 0) {
            role.setisMoving(true);
            //碰撞并且p2未移动并且都在地面
            if (checkCollisions(entity)
                    && !entity.getisMoving()
                    && entity.getCurrentY() == role.getCurrentY()) {
                pushCurrentX = role.getCurrentX() - 150 - speedX / 2;
                role.setCurrentX(role.getCurrentX() - speedX / 2);
            } else if (checkCollisions(entity)
                    && entity.getisMoving()
                    && entity.getCurrentY() == role.getCurrentY()) {
                role.setCurrentX(role.getPreX());
            } else {
                role.setCurrentX(role.getCurrentX() - speedX * 1.5);
            }
        } else if (InputManager.getPressed().contains(KeyCode.D)
                && role.getRoleBox().getRwidth() <= ComputerWidth + 1440.0D) {
            role.setisMoving(true);
            role.setCurrentX(role.getCurrentX() + speedX);
        }
    }

    //facing为1时的移动方法(即朝向为右)
    public void Facing1Move(parentRole entity) {
        if (InputManager.getPressed().contains(KeyCode.D)
                && role.getRoleBox().getRwidth() <= ComputerWidth + 1440.0D) {
            role.setisMoving(true);
            if (checkCollisions(entity)
                    && !entity.getisMoving()
                    && entity.getCurrentY() == role.getCurrentY()) {
                pushCurrentX = role.getCurrentX() + 150 + speedX / 2;
                role.setCurrentX(role.getCurrentX() + speedX / 2);
            } else if (checkCollisions(entity)
                    && entity.getisMoving()
                    && entity.getCurrentY() == role.getCurrentY()) {
                role.setCurrentX(role.getPreX());
            } else {
                role.setCurrentX(role.getCurrentX() + speedX * 1.5);
            }
        } else if (InputManager.getPressed().contains(KeyCode.A) && role.getRoleBox().getLwidth() >= 0) {
            role.setisMoving(true);
            role.setCurrentX(role.getCurrentX() - speedX);
        }
    }

    //角色跳跃落地挤压
    public void FloorSqueeze(parentRole entity) {
        if (role.getCurrentX() - entity.getCurrentX() <= 0
                && role.getCurrentX() - entity.getCurrentX() >= -150
                && role.getCurrentY() == entity.getCurrentY()) {
            role.setCurrentX(entity.getCurrentX() - 150);
        }
        if (role.getCurrentX() - entity.getCurrentX() >= 0
                && role.getCurrentX() - entity.getCurrentX() <= 150
                && role.getCurrentY() == entity.getCurrentY()) {
            role.setCurrentX(entity.getCurrentX() + 150);
        }
    }

    //垂直跳
    public void VerticalJump(parentRole entity) {
        if (role.getCurrentY() >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7) {
            role.setCurrentY((double) ComputerHeight / 2 + (double) ComputerHeight / 7);
            currentSpeedY = 0;
            isjumping = false;
            isjumped = true;
            FloorSqueeze(entity);
        }
    }

    //向左跳
    public void LeftJump(parentRole entity) {
        role.setCurrentX(role.getCurrentX() - speedX * 2);
        if (JunmpCollisions(entity) && role.getCurrentX() >= entity.getCurrentX()) {
            role.setCurrentX(role.getPreX());
        }
        if (role.getCurrentY() >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7) {
            role.setCurrentY((double) ComputerHeight / 2 + (double) ComputerHeight / 7);
            currentSpeedY = 0;
            isjumping = false;
            isjumped = true;
            FloorSqueeze(entity);
        }
    }

    //向右跳
    public void RightJump(parentRole entity) {
        role.setCurrentX(role.getCurrentX() + speedX * 2);
        if (JunmpCollisions(entity) && role.getCurrentX() <= entity.getCurrentX()) {
            role.setCurrentX(role.getPreX());
        }
        if (role.getCurrentY() >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7) {
            role.setCurrentY((double) ComputerHeight / 2 + (double) ComputerHeight / 7);
            currentSpeedY = 0;
            isjumping = false;
            isjumped = true;
            FloorSqueeze(entity);
        }
    }

    //碰撞检测
    public boolean checkCollisions(parentRole entity) {
        double instance = abs(role.getCurrentX() - entity.getCurrentX());
        return instance <= 150;

    }

    //跳跃碰撞检测
    public boolean JunmpCollisions(parentRole entity) {
        double instanceX = abs(role.getCurrentX() - entity.getCurrentX());
        double instanceY = abs(role.getCurrentY() - entity.getCurrentY());
        return instanceX <= 150 && instanceY == 0;
    }

    public double getPushCurrentX() {
        return pushCurrentX;
    }
}
